﻿using Less.Core;
using Less.Tools;
using Less.Core.Model;

namespace Less.DAL
{
    /// <summary>
    /// Data Access Layer to work with Roles Records
    /// </summary>
    internal class UsersRolesDAL : GenericDAL<UserRole>
    {
        // TODO: Extract SQL sentences to ConfigSettings
        internal UsersRolesDAL(IDatabase database)
        : base(database, "UsersRoles")
        {
        }

        internal UsersRolesDAL()
        : base(new Database(LessDatabaseUtils.LessConnectionStringName), "UsersRoles")
        {
        }

        /// <summary>
        /// Gets a UserRole Record 
        /// </summary>
        /// <param name="userId">The User Id</param>
        /// <param name="roleId">The Role Id</param>
        /// <returns>The UserRole Record </returns>
        public UserRole SelectByUserIdRoleId(long userId, long roleId)
        {
            UserRole record = null;
            record = Database.Select<UserRole>(string.Format("SELECT *  FROM {0} WHERE UserId=@0 ANd RoleId=@1", TableName), userId, roleId);
            return record;
        }

        /// <summary>
        /// Inserts a User Record 
        /// </summary>
        /// <param name="record">The User Record</param>
        public void Insert(UserRole record)
        {
            UserRole recordDB = SelectByUserIdRoleId(record.UserId, record.RoleId);
            if (recordDB == null)
                base.Insert(record);
            else
                throw new RecordAlreadyExistsException(record.Serialize());
        }

        /// <summary>
        /// Updates a User Record 
        ///  </summary>
        /// <param name="record">The User Record and its associated Entity Record</param>
        public void Update(UserRole record)
        {
            UserRole recordDB = SelectById(record.Id);
            if (recordDB != null)
                base.Update(record);
            else
                throw new RecordNotFoundException(record.Serialize());
        }

        /// <summary>
        /// Deletes a User Record 
        ///  </summary>
        /// <param name="record">The User Record and its associated Entity Record</param>
        public void Delete(UserRole record)
        {
            UserRole recordDB = SelectById(record.Id);
            if (recordDB != null)
                base.Delete(record);
            else
                throw new RecordNotFoundException(record.Serialize());
        }
        /// <summary>
        /// Determines if a user belongs to a role
        /// </summary>
        /// <param name="userId"></param>
        /// <param name="roleId"></param>
        /// <returns></returns>
        public bool IsUserInRole(int userId, int roleId)
        {
            bool exists = false;
            exists = Database.ExecuteScalar<int>(string.Format("SELECT Count(Id) FROM {0} WHERE UserId=@0 AND RoleId=@1", TableName), userId, roleId) > 0;
            return exists;
        }
    }
}